﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class BezierCurveMgr
{
    static public BezierCurveBase To( Transform transform, Vector3 p1, Vector3 p2, float duration, float disRatio, float heightRatio, float angle = 0)
    {
        return new SecondOrderBezierCurve(transform, p1, p2, duration, disRatio, heightRatio, angle);
    }
}

public class BezierCurveBase
{
    public System.Action onUpdate { get; set; }
    public System.Action onComplete { get; set; }
}

public class SecondOrderBezierCurve : BezierCurveBase
{
    Transform transform;
    public Vector3 p1;
    public Vector3 p2;
    public Vector3 center;
    public float duration;

    public float time = 0;
    public SecondOrderBezierCurve(Transform transform, Vector3 p1, Vector3 p2, float duration, float disRatio, float heightRatio, float angle = 0)
    {
        this.transform = transform;
        this.p1 = p1;
        this.p2 = p2;

        center = BezierCurveTools.CalculateCenterPoint(p1, p2, disRatio, heightRatio, angle);
        time = 0;
        Tween tween = DOTween.To(()=>time,x=>time=x,1, duration).SetEase( Ease.Linear );
        tween.onUpdate = OnUpdateTween;
        tween.onComplete = OnCompleteTween;
    }

    public void OnUpdateTween()
    {
        transform.position = BezierCurve.Bezier(p1, center, p2, time);
        if (null != onUpdate) { onUpdate(); }
    }


    public void OnCompleteTween()
    { 
        transform.position = p2;

        if (null != onComplete) { onComplete(); }
    }
}
